
# -*- coding: utf-8 -*-
# 仿真设计
# @Time    ： 2024/4/30  0:26
# @Auther  ： 徐良
# File     :  iconsPanel.py

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QWidget
from baseClass.baseQWidget import BaseQWidget
from baseUI.emulationSetUI import Ui_Form
from share.dataFactory import SqlForSave
from share.makeTree import MakeTree
from utils.pcWork import PCWork
import share.globalvar as gol

from share.bioDialog import Infomation

class IconsPanel(BaseQWidget, PCWork, Ui_Form):
    """
    pass
    """
    def __init__(self, msDict, parent=None):
        super(IconsPanel, self).__init__()
        self.queryPanel = None
        self.setAttribute(Qt.WA_StyledBackground)
        self.setupUi(self)
        self.toolbarFlag = 'browse'
        self.stackedW = "Yes"

        self.baseDbType = gol.getValue('baseDbType')
        msDict = eval(msDict)
        print(msDict)
        self.doInitWork(msDict)
        # self.initPara()
        # self.auditAfter.connect(self.onAuditAfter)


        # self.toolbarFinish = 0    # 工具栏设置完成状态，0 未完成，1，完成，由主控模块设置
        # self.toolbarStateTimer.timeout.connect(self.check_toolbarFinish)
        # self.toolbarStateTimer.start(500)

        self.toolbarFinish = 0  # 工具栏设置完成状态，0 未完成，1，完成，由主控模块设置


    def readData(self, sql):
        """
        读取数据
        :return:
        """
        pass

    def setLabel(self):

        self.lbl_editTitle.setText(self.FEditTitle)
        self.lbl_browseTitle.setText(self.FBrowseTitle)

    def actionSelect(self, q):
        sel = q.text()

        if sel == '增加':
            count = self.mainModel.rowCount()
            self.mainModel.insertRows(count)
            index = self.mainModel.index(count, 0)
            self.onTableViewClicked(index)
        elif sel == '插入':
            index = self.tableView.currentIndex()
            row = index.row()
            self.mainModel.insertRows(row)
            self.onTableViewClicked(index)
        elif sel == '删除':
            index = self.tableView.currentIndex()
            row = index.row()
            self.mainModel.removeRows(row)
            index = self.tableView.currentIndex()
            if index.isValid():
                self.onTableViewClicked(index)
        elif sel == '审核':
            self.audit(1)
        elif sel == '反审核':
            self.audit(0)
        elif sel == '刷新':
            self.readData(self.FMainSql)
        elif sel == '导出':
            print('导出')

    def audit(self, iFlag):
        """
        审核
        :return:
        """
        index = self.tableView.currentIndex()

        if not index.isValid():
            Infomation.info('请选定记录')
            return

        row = index.row()
        sfid = self.mainModel.record(0).value('FId')

        if iFlag:
            sVal = 'admin'
        else:
            sVal = ''
        # 保存到数据库
        sql = "update {tableName} set FChecker='{val}' where Fid={fid}"
        sql = sql.format(tableName=self.getA1_FTableName(), val=sVal, fid=str(sfid))
        ok = SqlForSave.execSqlList([sql], self.baseDbType)
        if ok['ret']:

            icol = self.getBrowseColumn('FChecker')
            icol2 = self.getBrowseColumn('FState')
            if iFlag:
                idx = self.mainModel.index(row, icol)
                self.mainModel.setData(idx, 'admin')

                idx = self.mainModel.index(row, icol2)
                self.mainModel.setData(idx, '已审核')
            else:
                idx = self.mainModel.index(row, icol)
                self.mainModel.setData(idx, '')
                idx = self.mainModel.index(row, icol2)
                self.mainModel.setData(idx, '未审核')

            self.auditAfter.emit({'row': row, 'iFlag': iFlag})


    def save(self):
        """
        保存单据
        """
        self.tableSave()


    def onTableViewClicked(self, index):
        if index.isValid():
            row = index.row()
            self.setBrowsePanel(row)
            self.mapper.setCurrentIndex(row)
            self.onRecordChanged(row)
            self.setToolbarButtons()



if __name__ == '__main__':
    import sys
    from PySide6.QtWidgets import QApplication, QDataWidgetMapper, QWidget

    app = QApplication(sys.argv)
    dic = "{'msFNumber': '05.01', 'msClass': '10', 'msSaveType':1}"
    window = IconsPanel(dic)
    window.show()
    sys.exit(app.exec())

